home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lan.zip / PUT.C < prev    next >
Text File  |  1991-07-18  |  3KB  |  179 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #define PORT 1
  4.  
  5. unsigned int filesize();
  6.  
  7. void sport();
  8. void send_file();
  9. void send_file_name();
  10. void port_init();
  11. void wait();
  12.  
  13. main (argc,argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.  if (argc != 2)
  18.  {
  19.   printf ("Usage: PUT <filename>");
  20.   exit (1);
  21.  }
  22.  port_init(PORT);
  23.  send_file(argv[1]);
  24. }
  25.  
  26. /*********************************/
  27. void send_file(fname)
  28. char *fname;
  29. {
  30.  FILE *fp;
  31.  char ch;
  32.  
  33.  union
  34.  {
  35.  char c[2];
  36.  unsigned int count;
  37.  } cnt;
  38.  if (!(fp=fopen(fname,"rb")))
  39.  {
  40.   printf ("Cannot open output file!");
  41.   exit (1);
  42.  }
  43.  printf ("Sending file: %s\n",fname);
  44.  
  45.  sport (PORT,'r');
  46.  wait (PORT);
  47.  
  48.  send_file_name(fname);
  49.  if (rport(PORT)!='.')
  50.  {
  51.   printf ("Remote file failure!\n");
  52.   exit (1);
  53.  }
  54.  cnt.count = filesize(fp);
  55.  
  56.  sport (PORT,cnt.c[0]);
  57.  wait (PORT);
  58.  sport (PORT,cnt.c[1]);
  59.  do
  60.  {
  61.   ch = getc(fp);
  62.   if (ferror(fp))
  63.   {
  64.    wait(PORT);
  65.    sport (PORT,ch);
  66.   }
  67.  }
  68.  while (!feof(fp));
  69.  wait (PORT);
  70.  fclose (fp);
  71. }
  72.  
  73. /**********************************/
  74. unsigned int filesize(fp)
  75. FILE *fp;
  76. {
  77.  unsigned long i;
  78.  i = 0;
  79.  do
  80.  {
  81.   getc(fp);
  82.   i++;
  83.  }
  84.  while (!feof(fp));
  85.  rewind(fp);
  86.  return i-1;
  87. }
  88.  
  89. /*********************************/
  90. void send_file_name(f)
  91. char *f;
  92. {
  93.  do
  94.  {
  95.   sport (PORT,'?');
  96.  }
  97.  while (!kbhit() && ! (check_stat(PORT) & 256));
  98.  if (kbhit())
  99.  {
  100.   getch();
  101.   exit (1);
  102.  }
  103.  wait(PORT);
  104.  while (*f)
  105.  {
  106.   sport(PORT,*f++);
  107.   wait(PORT);
  108.  }
  109.  sport(PORT,'\0');
  110.  wait(PORT);
  111. }
  112.  
  113. /*************************/
  114. void wait(port)
  115. int port;
  116. {
  117.  if (rport(port) != '.')
  118.  {
  119.   printf ("Communication error!=n");
  120.   exit (1);
  121.  }
  122. }
  123.  
  124. /******************************/
  125. void sport (port,c)
  126. int port;
  127. char c;
  128. {
  129.  union REGS r;
  130.  r.x.dx = port;
  131.  r.h.al = c;
  132.  r.h.ah = 1;
  133.  int86(0x14,&r,&r);
  134.  if (r.h.ah & 128)
  135.  {
  136.   printf ("Send error detected in serial port!\n");
  137.   exit(1);
  138.  }
  139. }
  140.  
  141. /*****************************/
  142. rport(port)
  143. int port;
  144. {
  145.  union REGS r;
  146.  while (!(check_stat(PORT) & 256))
  147.  if (kbhit())
  148.  {
  149.   getch();
  150.   exit(1);
  151.  }
  152.  r.x.dx = port;
  153.  r.h.ah = 2;
  154.  int86 (0x14,&r,&r);
  155.  if (r.h.ah & 128) printf ("Read error detected in the serial port!\n");
  156.  return r.h.al;
  157. }
  158.  
  159. /*******************************/
  160. check_stat(port)
  161. int port;
  162. {
  163.  union REGS r;
  164.  r.x.dx = port;
  165.  r.h.ah = 3;
  166.  int86 (0x14,&r,&r);
  167.  return r.x.ax;
  168. }
  169.  
  170. /*******************************/
  171. void port_init(port)
  172. int port;
  173. {
  174.  union REGS r;
  175.  r.x.dx = port;
  176.  r.h.ah = 0;
  177.  r.h.al = 251;
  178.  int86(0x14,&r,&r);
  179. }